package com.xmkj.bearuser.my;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.common.Entity.ShareOrderEntity;
import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.ShareOrderMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.my.adapter.ShareOrderAdapter;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

/**
 * Create by leo
 * 创建日期：2018/5/11
 * 描述：
 */

public class MyShareOrderActivity extends BaseMvpActivity {
    XRecyclerView recyclerview;
    private List<ShareOrderEntity.ListsEntity> bean = new ArrayList<>();
    private ShareOrderAdapter adapter;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_reclerview;
    }

    @Override
    protected void onViewCreated() {
        setRightNavigationText();
        recyclerview = findViewById(R.id.recyclerview);
    }
    /**
     * 设置右边文字
     */
    protected void setRightNavigationText() {
        m_navigationBar.setVisibility(View.VISIBLE);

    }

    @Override
    protected void doLogicFunc() {
        mPageSize = 20;
        gotoHttpRep();
        setRecyclerView();
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.REFRESH_CIRCLE)&&(boolean) rxKeyEvent.getValue()) {
                    mPageIndex = 1 ;
                    gotoHttpRep();
                }
            }
        }));

    }

    @Override
    protected void setNavigation() {
        setNavigationBack("我的晒单");
    }
    private void setRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 5)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
        adapter = new ShareOrderAdapter(context, bean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);
   /*     adapter.setOnItemChildClickListener(new CommonAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(CommonAdapter adapter, View view, int position) {
                infobean = (FriendCicleBean.ListBean) adapter.mData.get(position);
                if (view.getId() == R.id.ll_zan) { //点赞功能 或者取消点赞
                    gotoDianZan(infobean);
                } else if (view.getId() == R.id.ll_comment) {
                    commenttype = 1;
                    rl_et.setVisibility(View.VISIBLE);
                    EditTextUtil.showEditText(context, et_content);
                } else if(view.getId()==R.id.iv_gift){
                    //送礼物
                    Intent intent = new Intent(FriendCircleActivity.this,GiftShopDongTaiActivity.class);
                    intent.putExtra(GiftShopDongTaiActivity.INFO,infobean.uid);
                    intent.putExtra(GiftShopDongTaiActivity.DID,infobean.id);
                    context.startActivity(intent);

                }else if(view.getId()==R.id.iv_face){
                    Intent intent = new Intent(context,PersonDetatilsActivity.class);
                    intent.putExtra(PersonDetatilsActivity.FID,infobean.uid);
                    startActivity(intent);
                }
            }
        });*/
//        ClickListener(new CommonAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, Object item, int position) {

//            }
//        });


        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }

    private void gotoHttpRep() {
        CommonSubscriber<ShareOrderEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ShareOrderEntity beans = (ShareOrderEntity) o;
                List<ShareOrderEntity.ListsEntity> beanList = beans.lists;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        ShareOrderMethods.getInstance().saidanList(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), mPageIndex, 1);
        rxManager.add(subscriber);
    }
}
